package com.roncoo.chapter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roncoo.chapter.entity.EduChapterPeriod;
import com.roncoo.chapter.service.EduChapterPeriodService;
import com.roncoo.course.entity.EduCourse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wdf
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/eduChapterPeriod")
public class EduChapterPeriodController {
    @Resource
    private EduChapterPeriodService eduChapterPeriodService;
    @ApiOperation(value = "根据id查询 ")
    @PostMapping("findChapterPeriodById/{id}/{chid}")
    public List<EduChapterPeriod> findChapterPeriodById(@PathVariable("chid") Integer cid ,@PathVariable("id") Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",id);
        queryWrapper.eq("chapter_id",cid);
        return this.eduChapterPeriodService.list(queryWrapper);
    }

    @ApiOperation(value = "新增或修改 ")
    @PostMapping("update")
    public Boolean findChapterPeriodById(EduChapterPeriod eduChapterPeriod) throws ParseException {
        Date date = new Date();
        SimpleDateFormat sm2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String str = sm2.format(date);
        Date parse = sm2.parse(str);
        eduChapterPeriod.setModifiedTime(parse);
        if(eduChapterPeriod.getCreateTime() == null || eduChapterPeriod.getCreateTime()== "")
            eduChapterPeriod.setCreateTime(str);
        return this.eduChapterPeriodService.saveOrUpdate(eduChapterPeriod);
    }

    @ApiOperation(value = "修改视频播放时间 ")
    @PostMapping("updatebyTime")
    public Boolean updatebyTime(EduChapterPeriod eduChapterPeriod) {
        return this.eduChapterPeriodService.updateById(eduChapterPeriod);
    }

    @ApiOperation(value = "删除 ")
    @PostMapping("del")
    public Boolean delById(EduChapterPeriod eduChapterPeriod){
        return this.eduChapterPeriodService.removeById(eduChapterPeriod);
    }

    /**
     * 根据章节id查询课时
     */
    @GetMapping("findByCid")
    public List<EduChapterPeriod> findByCid(Integer cid){
        QueryWrapper<EduChapterPeriod> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",cid);
        return eduChapterPeriodService.list(queryWrapper);
    }
    /**
     * 获取阿里云地址
     */
    @PostMapping("getPath")
    public String getPath(MultipartFile file){
        return eduChapterPeriodService.getPath(file);
    }


    @ApiOperation(value = "根据课时id查询（主键）")
    @PostMapping("findByPid")
    @ResponseBody
    public EduChapterPeriod findByPid(EduChapterPeriod eduChapterPeriod){
        System.out.println(eduChapterPeriod);
//        if(eduCourse.getStatus() != null && eduCourse.getStatus() != -1)
//            queryWrapper.eq("status",eduCourse.getStatus());
        return eduChapterPeriodService.getById(eduChapterPeriod);
    }
    /**
     * 修改
     */
    @PostMapping("upd")
    public Boolean upd(EduChapterPeriod eduChapterPeriod){
        return eduChapterPeriodService.updateById(eduChapterPeriod);
    }
}

